package com.myebills.ebpsfrontend.cust.maxis;

import com.myebills.ebpsfrontend.cust.controller.*;
import com.myebills.ebpsfrontend.cust.utils.*;
import com.checkfree.isolutions.customer.persist.Account;
import com.checkfree.isolutions.customer.persist.User;
import com.checkfree.isolutions.admin.ObjectId;
import com.checkfree.isolutions.statement.persist.Statement;
import com.checkfree.isolutions.statement.StatementSection;
import java.util.*;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class CusSuppLineServlet_v1r00 extends EbppxTransactionServlet {

    /**
     *
     *
     */
    public void doTransaction(HttpServletRequest req, HttpServletResponse res)
    throws ServletException {
        try {
            final HttpSession session = req.getSession(false);
            if (null == session) {
                throw new ServletException("Session must be already established");
            }

    		final String randomKey = (String) session.getAttribute(EbppxConstants.RANDOM_KEY);

            final EbppxUrlManager urlManager = getUrlManager(req);

            //Get attribute from session for the logon user
            final EbppxEcsUser ecsUser = (EbppxEcsUser) session.getAttribute(EbppxConstants.ECS_USER);

            //get the vector of Accounts
            final Vector cfiAccounts = ecsUser.getAccounts();

			if (cfiAccounts.size()>0) {
				boolean found = false;
				final Account cfiAccount = (Account) cfiAccounts.elementAt(0);
                com.checkfree.isolutions.statement.persist.Statement cfiStatement =  StatementUtil.getStatement(cfiAccount,EbppxCusConstants.STMT_START_MONTH,0);
	            final StatementSection cfiRootSection = cfiStatement.getRootSection();
	            final String stmtIndex = "0";
	            String phoneNumber ="";

	            if(cfiRootSection!=null){
					StatementSection detailsOfChargesSection = cfiRootSection.getSection("DetailsOfChargesSection");
		            if(detailsOfChargesSection!=null){
			            int detailsOfChargesSectionCount = (int) detailsOfChargesSection.getSectionCount("PhoneNumberSection");
			            for (int index = 0; index < detailsOfChargesSectionCount; index++) {
							StatementSection detailsOfChargesRSection = detailsOfChargesSection.getRepeatSection("PhoneNumberSection", index);
							phoneNumber = detailsOfChargesRSection.getFieldText("PhoneNumber");

							// Format msisdn
							String msisdn = EbppxUtil.removeChar(phoneNumber,'(');
							msisdn = EbppxUtil.removeChar(msisdn,')');
							msisdn = EbppxUtil.nullToEmpty(EbppxUtil.removeChar(msisdn,':'));
							msisdn = "6"+msisdn;
							System.out.println("MSISDN is "+msisdn);

							if(ecsUser.getSuppPhoneNo()!=null && !ecsUser.getSuppPhoneNo().equals("")){
								if(ecsUser.getSuppPhoneNo().equals(msisdn)){
									found = true;
									break;
								}
							}
						}

					}

					if(found){
						// forward to the telephone summary url
						System.out.println("Found!!!!!!");
					}
				}



				req.setAttribute(EbppxConstants.CFI_ACCOUNT, cfiAccount);
				req.setAttribute(EbppxConstants.CFI_STATEMENT, cfiStatement);
				req.setAttribute("rootSection", cfiRootSection);
				req.setAttribute("statementSection", cfiRootSection.getSection("DetailsOfChargesSection"));
				req.setAttribute("stmtIndex", stmtIndex);


			}
            //forwardToMappedUrl(req, res, EbppxConstants.URL_JSP_PARAM);
        } catch (ServletException e) {
            // Just re-throw these
            throw e;
        } catch (Throwable t) {
            throw new ServletException(t.toString(), t);
        }
    }
}